#######################
#Team Analysis Code#####
######################

#####################
###Group 4 members#####
#Michelle Lin  ########
#Carlos Galian   ######
#Adanna Chukwuma   ####
#######################s

###############################################################################################
####Cash or Condition: Evidence from a Cash Transfer Experiment################################
####By Sarah Baird, Craig McIntosh, and Berk Ozler#############################################
#### The Quarterly Journal of Economics(2011) 126, 1709 -1753###################################
###############################################################################################


#######################
#Pre-analysis code##
#######################


#Set working directory
#setwd("C:/Users/Adanna/Dropbox/Replication- Cash or Conditional/Final Code Second Stage")
#setwd("C:/Users/adc785/Dropbox/Replication- Cash or Conditional/Final Code Second Stage")
load("C:/Users/adc785/Dropbox/Replication- Cash or Conditional/Final Code Second Stage/dataset.RData")


#open packages
library(car)
library(MASS)
library(Zelig)
library(survey)
library(ggplot2)



#load dataset
#load("C:/Users/Adanna/Dropbox/Replication- Cash or Conditional/Final Code Second Stage/dataset.RData")
#load("C:/Users/adc785/Dropbox/Replication- Cash or Conditional/Final Code Second Stage/dataset.RData")

#lowest to highest treatment category for ordinal monthly amounts
dataset$monthly_money_T2a<-as.numeric(dataset$monthly_money_T2a)

dataset$monthly_money_T2b<-as.numeric(dataset$monthly_money_T2b)




#create dataset for school sample in round 3(refer to Appendix C)
small.data<-subset(dataset,SS.sample.R3!= "NA" & inschool.term1.2009.SS!="NA"& 
                     inschool.term2.2009.SS !="NA" & inschool.term3.2009.SS !="NA"  
                   & interm1 !="NA" & interm2 !="NA" & interm3!="NA")

#we simulate effect across monthly range for a 14 year old girl, in stratum 1, 
#with mean asset index at baseline, median highest grade at baseline and 
#who has not had sex at baseline

#QOI: first difference in probability of enrollment between highest and lowest treated amount for each term




###################################################################################################################
#ANALYSIS OF ORDINAL EFFECTS OF INCREASING MONTHLY AMOUNTS ON NUMBER OF TERMS ENROLLED#############################
#                               TEACHERS' REPORTS
###################################################################################################################

#First Differences: E(Y|X1) - E(Y|X) 

############################################################################
#                     FIRST TERM 2008  - TEACHERS                         #
############################################################################

#########                 CONDITIONAL CASH TRANSFER

data1<-subset(dataset, inschool.term1.2008.SS !="NA")


model.1a<-zelig(inschool.term1.2008.SS ~ monthly_money_T2a + .Iage.R1.14 + .Iage.R1.15 + .Iage.R1.16 + .Iage.R1.17 + .Iage.R1.18
                + .Iage.R1.19 + .Iage.R1.20 + stratum1+ stratum2 + asset.index.baseline + highest.grade.baseline
                +never.had.sex.baseline, model='logit.survey', data=data1, weights=~wgt,ids=~eaid, cite=F)#the second group of weights kick in t1.2009

# Predicted values

x.low <- setx(model.1a, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
              .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
              asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
              never.had.sex.baseline=1, monthly_money_T2a=1)


x.high <- setx(model.1a, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
              .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
              asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
              never.had.sex.baseline=1, monthly_money_T2a=11)

s.out1a <- sim(model.1a, x=x.low, x1=x.high)


plot(s.out1a)

summary(s.out1a)# 0.031


mean1a<-mean(s.out1a[["qi"]][[5]])

#########                 UNCONDITIONAL CASH TRANSFER


model.1b<-zelig(inschool.term1.2008.SS ~ monthly_money_T2b + .Iage.R1.14 + .Iage.R1.15 + .Iage.R1.16 + .Iage.R1.17 + .Iage.R1.18
                + .Iage.R1.19 + .Iage.R1.20 + stratum1+ stratum2 + asset.index.baseline + highest.grade.baseline
                +never.had.sex.baseline, model='logit.survey', data=data1, weights=~wgt,ids=~eaid, cite=F)#the second group of weights kick in t1.2009

# Predicted values

x.low <- setx(model.1b, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
              .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
              asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
              never.had.sex.baseline=1, monthly_money_T2b=1)


x.high <- setx(model.1b, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
               .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
               asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
               never.had.sex.baseline=1, monthly_money_T2b=11)

s.out1b <- sim(model.1b, x=x.low, x1=x.high)


plot(s.out1b)

summary(s.out1b)#0.003


rm(data1)


############################################################################
#                     SECOND TERM 2008  - TEACHERS                                    #
############################################################################

#########                 CONDITIONAL CASH TRANSFER



data1<-subset(dataset, inschool.term2.2008.SS !="NA")


model.2a<-zelig(inschool.term2.2008.SS ~ monthly_money_T2a + .Iage.R1.14 + .Iage.R1.15 + .Iage.R1.16 + .Iage.R1.17 + .Iage.R1.18
                + .Iage.R1.19 + .Iage.R1.20 + stratum1+ stratum2 + asset.index.baseline + highest.grade.baseline
                +never.had.sex.baseline, model='logit.survey', data=data1, weights=~wgt,ids=~eaid, cite=F)#the second group of weights kick in t1.2009

# Predicted values

x.low <- setx(model.2a, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
              .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
              asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
              never.had.sex.baseline=1, monthly_money_T2a=1)


x.high <- setx(model.2a, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
               .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
               asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
               never.had.sex.baseline=1, monthly_money_T2a=11)

s.out2a <- sim(model.2a, x=x.low, x1=x.high)


plot(s.out2a)#0.026

summary(s.out2a)



#########                 UNCONDITIONAL CASH TRANSFER


model.2b<-zelig(inschool.term2.2008.SS ~ monthly_money_T2b + .Iage.R1.14 + .Iage.R1.15 + .Iage.R1.16 + .Iage.R1.17 + .Iage.R1.18
                + .Iage.R1.19 + .Iage.R1.20 + stratum1+ stratum2 + asset.index.baseline + highest.grade.baseline
                +never.had.sex.baseline, model='logit.survey', data=data1, weights=~wgt,ids=~eaid, cite=F)#the second group of weights kick in t1.2009

# Predicted values

x.low <- setx(model.2b, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
              .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
              asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
              never.had.sex.baseline=1, monthly_money_T2b=1)


x.high <- setx(model.2b, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
               .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
               asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
               never.had.sex.baseline=1, monthly_money_T2b=11)

s.out2b <- sim(model.2b, x=x.low, x1=x.high)


plot(s.out2b)

summary(s.out2b)#0.015


rm(data1)


############################################################################
#                     THIRD TERM 2008  - TEACHERS                                    #
############################################################################

#########                 CONDITIONAL CASH TRANSFER



data1<-subset(dataset, inschool.term3.2008.SS !="NA")


model.3a<-zelig(inschool.term3.2008.SS ~ monthly_money_T2a + .Iage.R1.14 + .Iage.R1.15 + .Iage.R1.16 + .Iage.R1.17 + .Iage.R1.18
                + .Iage.R1.19 + .Iage.R1.20 + stratum1+ stratum2 + asset.index.baseline + highest.grade.baseline
                +never.had.sex.baseline, model='logit.survey', data=data1, weights=~wgt,ids=~eaid, cite=F)#the second group of weights kick in t1.2009

# Predicted values

x.low <- setx(model.3a, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
              .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
              asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
              never.had.sex.baseline=1, monthly_money_T2a=1)


x.high <- setx(model.3a, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
               .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
               asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
               never.had.sex.baseline=1, monthly_money_T2a=11)

s.out3a <- sim(model.3a, x=x.low, x1=x.high)


plot(s.out3a)# 0.043

summary(s.out3a)



#########                 UNCONDITIONAL CASH TRANSFER


model.3b<-zelig(inschool.term3.2008.SS ~ monthly_money_T2b + .Iage.R1.14 + .Iage.R1.15 + .Iage.R1.16 + .Iage.R1.17 + .Iage.R1.18
                + .Iage.R1.19 + .Iage.R1.20 + stratum1+ stratum2 + asset.index.baseline + highest.grade.baseline
                +never.had.sex.baseline, model='logit.survey', data=data1, weights=~wgt,ids=~eaid, cite=F)#the second group of weights kick in t1.2009

# Predicted values

x.low <- setx(model.3b, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
              .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
              asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
              never.had.sex.baseline=1, monthly_money_T2b=1)


x.high <- setx(model.3b, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
               .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
               asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
               never.had.sex.baseline=1, monthly_money_T2b=11)

s.out3b <- sim(model.3b, x=x.low, x1=x.high)


plot(s.out3b)

summary(s.out3b)#-0.012


rm(data1)

############################################################################
#                     FIRST TERM 2009  - TEACHERS                                    #
############################################################################


#########                 CONDITIONAL CASH TRANSFER



data1<-subset(small.data, inschool.term1.2009.SS !="NA")


model.4a<-zelig(inschool.term1.2009.SS ~ monthly_money_T2a + .Iage.R1.14 + .Iage.R1.15 + .Iage.R1.16 + .Iage.R1.17 + .Iage.R1.18
                + .Iage.R1.19 + .Iage.R1.20 + stratum1+ stratum2 + asset.index.baseline + highest.grade.baseline
                +never.had.sex.baseline, model='logit.survey', data=data1, weights=~wgt.SSR3,ids=~eaid, cite=F)#the second group of weights kick in t1.2009

# Predicted values

x.low <- setx(model.4a, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
              .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
              asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
              never.had.sex.baseline=1, monthly_money_T2a=1)


x.high <- setx(model.4a, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
               .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
               asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
               never.had.sex.baseline=1, monthly_money_T2a=11)

s.out4a <- sim(model.4a, x=x.low, x1=x.high)


plot(s.out4a)# 0.034

summary(s.out4a)



#########                 UNCONDITIONAL CASH TRANSFER


model.4b<-zelig(inschool.term1.2009.SS ~ monthly_money_T2b + .Iage.R1.14 + .Iage.R1.15 + .Iage.R1.16 + .Iage.R1.17 + .Iage.R1.18
                + .Iage.R1.19 + .Iage.R1.20 + stratum1+ stratum2 + asset.index.baseline + highest.grade.baseline
                +never.had.sex.baseline, model='logit.survey', data=data1, weights=~wgt.SSR3,ids=~eaid, cite=F)#the second group of weights kick in t1.2009

# Predicted values

x.low <- setx(model.4b, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
              .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
              asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
              never.had.sex.baseline=1, monthly_money_T2b=1)


x.high <- setx(model.4b, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
               .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
               asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
               never.had.sex.baseline=1, monthly_money_T2b=11)

s.out4b <- sim(model.4b, x=x.low, x1=x.high)


plot(s.out4b)

summary(s.out4b)#-0.041

rm(data1)

############################################################################
#                     SECOND TERM 2009  - TEACHERS                         #
############################################################################

#########                 CONDITIONAL CASH TRANSFER



data1<-subset(small.data, inschool.term2.2009.SS !="NA")


model.5a<-zelig(inschool.term2.2009.SS ~ monthly_money_T2a + .Iage.R1.14 + .Iage.R1.15 + .Iage.R1.16 + .Iage.R1.17 + .Iage.R1.18
                + .Iage.R1.19 + .Iage.R1.20 + stratum1+ stratum2 + asset.index.baseline + highest.grade.baseline
                +never.had.sex.baseline, model='logit.survey', data=data1, weights=~wgt.SSR3,ids=~eaid, cite=F)#the second group of weights kick in t1.2009

# Predicted values

x.low <- setx(model.5a, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
              .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
              asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
              never.had.sex.baseline=1, monthly_money_T2a=1)


x.high <- setx(model.5a, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
               .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
               asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
               never.had.sex.baseline=1, monthly_money_T2a=11)

s.out5a <- sim(model.5a, x=x.low, x1=x.high)


plot(s.out5a)#  0.079

summary(s.out5a)



#########                 UNCONDITIONAL CASH TRANSFER


model.5b<-zelig(inschool.term2.2009.SS ~ monthly_money_T2b + .Iage.R1.14 + .Iage.R1.15 + .Iage.R1.16 + .Iage.R1.17 + .Iage.R1.18
                + .Iage.R1.19 + .Iage.R1.20 + stratum1+ stratum2 + asset.index.baseline + highest.grade.baseline
                +never.had.sex.baseline, model='logit.survey', data=data1, weights=~wgt.SSR3,ids=~eaid, cite=F)#the second group of weights kick in t1.2009

# Predicted values

x.low <- setx(model.5b, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
              .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
              asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
              never.had.sex.baseline=1, monthly_money_T2b=1)


x.high <- setx(model.5b, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
               .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
               asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
               never.had.sex.baseline=1, monthly_money_T2b=11)

s.out5b <- sim(model.5b, x=x.low, x1=x.high)


plot(s.out5b)

summary(s.out5b)#-0.076

rm(data1)

############################################################################
#                     THIRD TERM 2009  - TEACHERS                                    #
############################################################################

#########                 CONDITIONAL CASH TRANSFER



data1<-subset(small.data, inschool.term3.2009.SS !="NA")


model.6a<-zelig(inschool.term3.2009.SS ~ monthly_money_T2a + .Iage.R1.14 + .Iage.R1.15 + .Iage.R1.16 + .Iage.R1.17 + .Iage.R1.18
                + .Iage.R1.19 + .Iage.R1.20 + stratum1+ stratum2 + asset.index.baseline + highest.grade.baseline
                +never.had.sex.baseline, model='logit.survey', data=data1, weights=~wgt.SSR3,ids=~eaid, cite=F)#the second group of weights kick in t1.2009

# Predicted values

x.low <- setx(model.6a, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
              .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
              asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
              never.had.sex.baseline=1, monthly_money_T2a=1)


x.high <- setx(model.6a, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
               .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
               asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
               never.had.sex.baseline=1, monthly_money_T2a=11)

s.out6a <- sim(model.6a, x=x.low, x1=x.high)


plot(s.out6a)#  0.073

summary(s.out6a)



#########                 UNCONDITIONAL CASH TRANSFER


model.6b<-zelig(inschool.term3.2009.SS ~ monthly_money_T2b + .Iage.R1.14 + .Iage.R1.15 + .Iage.R1.16 + .Iage.R1.17 + .Iage.R1.18
                + .Iage.R1.19 + .Iage.R1.20 + stratum1+ stratum2 + asset.index.baseline + highest.grade.baseline
                +never.had.sex.baseline, model='logit.survey', data=data1, weights=~wgt.SSR3,ids=~eaid, cite=F)#the second group of weights kick in t1.2009

# Predicted values

x.low <- setx(model.6b, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
              .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
              asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
              never.had.sex.baseline=1, monthly_money_T2b=1)


x.high <- setx(model.6b, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
               .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
               asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
               never.had.sex.baseline=1, monthly_money_T2b=11)

s.out6b <- sim(model.6b, x=x.low, x1=x.high)


plot(s.out6b)

summary(s.out6b)# -0.065

rm(data1)

############################################################################
#                     FIRST TERM 2010  - TEACHERS                                    #
############################################################################

#########                 CONDITIONAL CASH TRANSFER

#########                 CONDITIONAL CASH TRANSFER



data1<-subset(small.data, inschool.term1.2010.SS !="NA")


model.7a<-zelig(inschool.term1.2010.SS ~ monthly_money_T2a + .Iage.R1.14 + .Iage.R1.15 + .Iage.R1.16 + .Iage.R1.17 + .Iage.R1.18
                + .Iage.R1.19 + .Iage.R1.20 + stratum1+ stratum2 + asset.index.baseline + highest.grade.baseline
                +never.had.sex.baseline, model='logit.survey', data=data1, weights=~wgt.SSR3,ids=~eaid, cite=F)#the second group of weights kick in t1.2009

# Predicted values

x.low <- setx(model.7a, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
              .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
              asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
              never.had.sex.baseline=1, monthly_money_T2a=1)


x.high <- setx(model.7a, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
               .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
               asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
               never.had.sex.baseline=1, monthly_money_T2a=11)

s.out7a <- sim(model.7a, x=x.low, x1=x.high)


plot(s.out7a)#  0.124

summary(s.out7a)



#########                 UNCONDITIONAL CASH TRANSFER


model.7b<-zelig(inschool.term1.2010.SS ~ monthly_money_T2b + .Iage.R1.14 + .Iage.R1.15 + .Iage.R1.16 + .Iage.R1.17 + .Iage.R1.18
                + .Iage.R1.19 + .Iage.R1.20 + stratum1+ stratum2 + asset.index.baseline + highest.grade.baseline
                +never.had.sex.baseline, model='logit.survey', data=data1, weights=~wgt.SSR3,ids=~eaid, cite=F)#the second group of weights kick in t1.2009

# Predicted values

x.low <- setx(model.7b, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
              .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
              asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
              never.had.sex.baseline=1, monthly_money_T2b=1)


x.high <- setx(model.7b, .Iage.R1.14=1,.Iage.R1.15=0,.Iage.R1.16=0,.Iage.R1.17=0,.Iage.R1.18=0,
               .Iage.R1.19=0,.Iage.R1.20=0,stratum1=1,stratum2=0,highest.grade.baseline=median(dataset$highest.grade.baseline),
               asset.index.baseline=mean(dataset$asset.index.baseline,na.rm=T),
               never.had.sex.baseline=1, monthly_money_T2b=11)

s.out7b <- sim(model.7b, x=x.low, x1=x.high)


plot(s.out7b)

summary(s.out7b)# -0.083

################################################################################################################
#################################### PLOTS #####################################################################
################################################################################################################

##################################################################################### #

##SUPPLEMENTARY PLOTS



first.difference <- data.frame(matrix(vector(), 14,3, dimnames=list(c(),c("difference","treatment","term"))))

first.difference<-data.frame(first.difference,as.numeric(first.difference$difference))

UCT.fd<-c(mean(s.out1b[["qi"]][[5]]),mean(s.out2b[["qi"]][[5]]),mean(s.out3b[["qi"]][[5]])
          ,mean(s.out4b[["qi"]][[5]]),mean(s.out5b[["qi"]][[5]]),mean(s.out6b[["qi"]][[5]]),mean(s.out7b[["qi"]][[5]]))

CCT.fd<-c(mean(s.out1a[["qi"]][[5]]),mean(s.out2a[["qi"]][[5]]),mean(s.out3a[["qi"]][[5]])
          ,mean(s.out4a[["qi"]][[5]]),mean(s.out5a[["qi"]][[5]]),mean(s.out6a[["qi"]][[5]]),mean(s.out7a[["qi"]][[5]]))


first.difference$term[1:7]<-c(1,2,3,4,5,6,7)
first.difference$difference[1:7]<-UCT.fd
first.difference$treatment[1:7]<-"UCT"
c("2008.t1", "2008.t2","2008.t3","2009.t1",
  "2009.t2","2009.t3","2010.t1")
first.difference$term[8:14]<-c(1,2,3,4,5,6,7)
first.difference$difference[8:14]<-CCT.fd
first.difference$treatment[8:14]<-"CCT"

qplot(data=first.difference,x=term,y=difference,color=treatment,ylim=c(0,0.125),
      main="Difference in expected enrollment by maximum and mininum transfer")
ggsave("First difference.jpg",scale=1.5)

ggplot(aes(y = difference, x = term, colour = treatment), data = first.difference)+geom_point()+geom_line()
ggsave("First difference - lines.jpg",scale=1.5)

##############

first.difference2 <- data.frame(matrix(vector(), 14000,3, dimnames=list(c(),c("difference","treatment","term"))))

first.difference2<-data.frame(first.difference2,as.numeric(first.difference2$difference))
first.difference2<-data.frame(first.difference2,as.factor(first.difference2$term))

UCT.fd<-c(s.out1b[["qi"]][[5]],s.out2b[["qi"]][[5]],s.out3b[["qi"]][[5]]
          ,s.out4b[["qi"]][[5]],s.out5b[["qi"]][[5]],s.out6b[["qi"]][[5]],s.out7b[["qi"]][[5]])

CCT.fd<-c(s.out1a[["qi"]][[5]],s.out2a[["qi"]][[5]],s.out3a[["qi"]][[5]]
          ,s.out4a[["qi"]][[5]],s.out5a[["qi"]][[5]],s.out6a[["qi"]][[5]],s.out7a[["qi"]][[5]])

first.difference2$term[1:1000]<-"2008.t1"
first.difference2$term[1001:2000]<-"2008.t2"
first.difference2$term[2001:3000]<-"2008.t3"
first.difference2$term[3001:4000]<-"2009.t1"
first.difference2$term[4001:5000]<-"2009.t2"
first.difference2$term[5001:6000]<-"2009.t3"
first.difference2$term[6001:7000]<-"2010.t1"
first.difference2$difference[1:7000]<-UCT.fd
first.difference2$treatment[1:7000]<-"UCT"

first.difference2$term[7001:8000]<-"2008.t1"
first.difference2$term[8001:9000]<-"2008.t2"
first.difference2$term[9001:10000]<-"2008.t3"
first.difference2$term[10001:11000]<-"2009.t1"
first.difference2$term[11001:12000]<-"2009.t2"
first.difference2$term[12001:13000]<-"2009.t3"
first.difference2$term[13001:14000]<-"2010.t1"
first.difference2$difference[7001:14000]<-CCT.fd
first.difference2$treatment[7001:14000]<-"CCT"

ggplot(aes(y = difference, x = term, fill = treatment), data = first.difference2,
       main="Box plot for difference in enrollment by max and min transfer") + geom_boxplot()

ggsave("Boxplot.png",scale=2)
#################################################################################

first.difference2 <- data.frame(matrix(vector(), 14000,3, dimnames=list(c(),c("difference","treatment","term"))))

first.difference2<-data.frame(first.difference2,as.numeric(first.difference2$difference))

UCT.fd<-c(s.out1b[["qi"]][[5]],s.out2b[["qi"]][[5]],s.out3b[["qi"]][[5]]
          ,s.out4b[["qi"]][[5]],s.out5b[["qi"]][[5]],s.out6b[["qi"]][[5]],s.out7b[["qi"]][[5]])

CCT.fd<-c(s.out1a[["qi"]][[5]],s.out2a[["qi"]][[5]],s.out3a[["qi"]][[5]]
          ,s.out4a[["qi"]][[5]],s.out5a[["qi"]][[5]],s.out6a[["qi"]][[5]],s.out7a[["qi"]][[5]])

first.difference2$term[1:1000]<-1
first.difference2$term[1001:2000]<-2
first.difference2$term[2001:3000]<-3
first.difference2$term[3001:4000]<-4
first.difference2$term[4001:5000]<-5
first.difference2$term[5001:6000]<-6
first.difference2$term[6001:7000]<-7
first.difference2$difference[1:7000]<-UCT.fd
first.difference2$treatment[1:7000]<-"UCT"

first.difference2$term[7001:8000]<-1
first.difference2$term[8001:9000]<-2
first.difference2$term[9001:10000]<-3
first.difference2$term[10001:11000]<-4
first.difference2$term[11001:12000]<-5
first.difference2$term[12001:13000]<-6
first.difference2$term[13001:14000]<-7
first.difference2$difference[7001:14000]<-CCT.fd
first.difference2$treatment[7001:14000]<-"CCT"


ggplot(data = first.difference2, aes(y = difference, x = term, colour = treatment))+
geom_smooth(method="loess",size=1)+ scale_x_discrete(breaks=c(1,2,3,4,5,6,7), labels=c("t1-2008", "t2-2008", "t3-2008","t1-2009","t2-2009","t3-2009", "t1-2010"))+                                                                                         
xlab("School Term")+
ylab("Difference in Probability of Enrollment")+
ggtitle(expression(atop("Difference in Enrollment Probability for CCT and UCT ", atop(italic("Difference for Highest and Lowest Transfer"))))) +
ggsave("Trend lines.png", scale=1.45)


